clear all; close all; clc;

R = 220;
C =33e-12;
L=100e-6;

RC=R*C;%���������� ������� ����
%��� �������
f0=1/(2*pi*sqrt(L*C));
T = 1/ (10000*f0);
t = 0:T:(15*1/f0);
it=length(t);
%�������� ������
Uc = nan(1, it);
Ul = nan(1, it);
dUc = nan(1, it);
i = nan(1, it);
di = nan(1, it);

E  = 0*(t<T/4) + 3*(t>=T/4);
%������ ��������� ��������� 
U(1) = E(1); dU(1) = 0;
%% ���
fmin = 1/ (T/2);
fmax = 2 *  (1/ RC /  2 / pi);
f = fmin:((fmax-fmin)/100):fmax;
A = 1;
K = nan(1, length(f));
for j = 1:length(f)
    E  = A*sin(2*pi*f(j)*t);
    
    U = nan(1, length(t));
    dU = nan(1, length(t));
    
    U(1) = E(1); dU(1) = 0;
    
    for k = 2:it

    Ul(k)=E(k)-i(k-1)*R-Uc(k-1);
    di(k)=Ul(k)/L;
    i(k)=i(k-1)+di(k-1)*T;
    dUc(k)=i(k)/C;
    Uc(k)=Uc(k-1)+dUc(k)*T;
    end
    
    Us = U(fix(end/2):end);
    K(j) = (max(Us) - min(Us)) / 2;
end

figure;
plot(f, 20*log10(K))
xlabel('f, Hz');
ylabel('K, dB');
grid on
figure;
plot(f, K)
xlabel('f, Hz');
ylabel('K');
grid on




